package com.qianyu.project.web.controller.hyh;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.qianyu.project.core.annotation.FlowBegin;
import com.qianyu.project.core.annotation.FlowEntry;
import com.qianyu.project.entity.LeaseContract;
import com.qianyu.project.entity.LeaseContractDetail;
import com.qianyu.project.service.ILeaseContractService;
import com.qianyu.project.utils.Result;
import com.qianyu.project.utils.ResultTool;
import com.qianyu.project.vo.LeaseContractVo;
import lombok.AllArgsConstructor;
import org.apache.poi.ss.formula.functions.Odd;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author hyh
 * @date 2021/2/314:19
 */
@RestController
@RequestMapping("leaseContract")
public class LeaseContractController {
    @Autowired
    ILeaseContractService iLeaseContractService;
    @GetMapping("")
    public Result getMaxid(){
        return ResultTool.success(Long.parseLong(iLeaseContractService.getMaxid()) + 1);

    }

    @PostMapping("")
    public  Result  saveLeaseContract(@RequestBody LeaseContractVo leaseContractVo){
        iLeaseContractService.saveLease(leaseContractVo.getLeaseContract(),leaseContractVo.getLeaseContractDetails());
        return ResultTool.success();
    }

    @GetMapping("/getLeaseContract")
    public List<LeaseContract> getLeaseContract(@RequestParam Map<String,String> map){
        QueryWrapper qw=new QueryWrapper();
        String id=map.get("id");
        if(StringUtils.isNotBlank(id)){
            qw.eq("id",id);
        }
        String contract_amount=map.get("contract_amount");
        String contract_amount2=map.get("contract_amount2");

        if(StringUtils.isNotBlank(contract_amount)&&StringUtils.isNotBlank(contract_amount2)){
            qw.between("contract_amount",contract_amount,contract_amount2);
        }

        String pid=map.get("pid");
        if(StringUtils.isNotBlank(pid)){
            qw.eq("p_id",pid);

        }
        String contract_signatory=map.get("contract_signatory");
        if(StringUtils.isNotBlank(contract_signatory)){
            qw.like("contract_signatory",contract_signatory);
        }
        String order=map.get("order");
        if(StringUtils.isNotBlank(order)){
            String[] orderargs=order.split("\\.");
            if(orderargs[1].equals("desc")){
                qw.orderByDesc(orderargs[0]);
            }else{
                qw.orderByAsc(orderargs[0]);
            }
        }
        String status=map.get("approve_status");
        if(StringUtils.isNotBlank(status)){
            qw.eq("approve_status",status);
        }


        System.out.println(map);
        return iLeaseContractService.list(qw);
    }

    @DeleteMapping("/{id}")
    public void  delLeaseContract(@PathVariable String id){
        System.out.println(id);
        iLeaseContractService.removeById(id);
    }


}
